﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace eTraining
{
    public class ControlBoxTool
    {
        private const int MinWidth = 18;
        private const int MaxWidth = 18;
        private const int CloseWidth = 30;
        private const int BtnHeight = 18;

        public static int LeftStartX
        {
            get
            {
                return MinWidth + MaxWidth + CloseWidth + BtnHeight;
            }
        }

        public void Draw(Graphics g, TopPanel pnl)
        {
            Point pt = GetMousePoint(pnl);

            Rectangle rectMin = new Rectangle(
                pnl.Width - MinWidth - MaxWidth - CloseWidth - BtnHeight,
                0,
                MinWidth,
                BtnHeight
            );
                        
            DrawButton(g, rectMin,rectMin.Contains(pt));
            DrawMin(g, rectMin);

            Rectangle rectMax =new Rectangle(
                pnl.Width  - MaxWidth - CloseWidth - BtnHeight,
                0,
                MaxWidth,
                BtnHeight
            );

            DrawButton(g, rectMax, rectMax.Contains(pt));
            
            DrawMax(g, rectMax,pnl.FindForm().WindowState == FormWindowState.Maximized);

            Rectangle rectClose = new Rectangle(
                pnl.Width - CloseWidth - BtnHeight,
                0,
                CloseWidth,
                BtnHeight
            );

            DrawButton(g, rectClose, rectClose.Contains(pt));
            DrawClose(g, rectClose);
        }

        private void DrawMin(Graphics g, Rectangle rect)
        {
            int w = rect.Width / 2;
            int h = rect.Height / 5;

            Rectangle rectDraw = new Rectangle(
                rect.Left + (rect.Width - w) / 2,
                rect.Top + (rect.Height - h) / 2,
                w,
                h
            );

            g.FillRectangle(Brushes.SkyBlue,rectDraw);
            g.DrawRectangle(Pens.Gray, rectDraw);
        }

        private void DrawClose(Graphics g, Rectangle rect)
        {
            int w = rect.Height / 2 + 4;
            int h = w;

            Rectangle rectDraw = new Rectangle(
                rect.Left + (rect.Width - w) / 2,
                rect.Top + (rect.Height - h) / 2,
                w,
                h
            );

            SmoothingMode sm = g.SmoothingMode;
            g.SmoothingMode = SmoothingMode.HighQuality;

            g.FillEllipse(Brushes.Brown, rectDraw);
            g.DrawEllipse(Pens.Gray, rectDraw);

            rectDraw.Offset(5, 3);
            rectDraw.Width = rectDraw.Width - 10;
            rectDraw.Height = rectDraw.Height - 6;

            g.FillEllipse(Brushes.Yellow, rectDraw);
            //g.DrawEllipse(Pens.Yellow, rectDraw);

            g.SmoothingMode = sm;
        }

        private void DrawMax(Graphics g, Rectangle rect,bool flg)
        {
            if (flg == true)
            {
                int w = rect.Width / 2;
                int h = rect.Height / 2;

                Rectangle rectDraw = new Rectangle(
                    rect.Left + (rect.Width - w) / 2,
                    rect.Top + (rect.Height - h) / 2,
                    w,
                    h
                );

                rectDraw.Offset(2, -2);
                g.FillRectangle(Brushes.SkyBlue, rectDraw);
                g.DrawRectangle(Pens.Gray, rectDraw);

                rectDraw.Offset(-4, 4);
                g.FillRectangle(Brushes.SkyBlue, rectDraw);
                g.DrawRectangle(Pens.Gray, rectDraw);
            }
            else
            {
                int w = rect.Width / 2 + 1;
                int h = rect.Height / 2 + 1;

                Rectangle rectDraw = new Rectangle(
                    rect.Left + (rect.Width - w) / 2,
                    rect.Top + (rect.Height - h) / 2,
                    w,
                    h
                );

                g.FillRectangle(Brushes.SkyBlue, rectDraw);
                g.DrawRectangle(Pens.Gray, rectDraw);
            }
        }


        public void DrawButton(Graphics g, Rectangle rect,bool HighLinghFlg)
        {
            rect.Offset(0, -1);

            if( HighLinghFlg == true)
            {
                SolidBrush brush = new SolidBrush(Color.FromArgb(80, Color.White));
                g.FillRectangle(brush, rect);
                brush.Dispose();

            }

            g.DrawRectangle(Pens.DarkGray,rect);
            g.DrawLine(Pens.WhiteSmoke, rect.Left + 1, rect.Top, rect.Left + 1, rect.Bottom -1);
            g.DrawLine(Pens.WhiteSmoke, rect.Left + 1, rect.Bottom + 1, rect.Right, rect.Bottom + 1);
            g.DrawLine(Pens.WhiteSmoke, rect.Right + 1, rect.Top, rect.Right + 1, rect.Bottom - 1);
        }

        private Point GetMousePoint(TopPanel pnl)
        {
            Point rst = pnl.PointToClient(Control.MousePosition);
            return rst;
        }

        public int HitTest(TopPanel pnl)
        {
            int rst = -1;

            Point pt = GetMousePoint(pnl);

            Rectangle rectMin = new Rectangle(
                pnl.Width - MinWidth - MaxWidth - CloseWidth - BtnHeight,
                0,
                MinWidth,
                BtnHeight
            );

            Rectangle rectMax = new Rectangle(
                pnl.Width - MaxWidth - CloseWidth - BtnHeight,
                0,
                MaxWidth,
                BtnHeight
            );

            Rectangle rectClose = new Rectangle(
                pnl.Width - CloseWidth - BtnHeight,
                0,
                CloseWidth,
                BtnHeight
            );

            if (rectMin.Contains(pt) == true)
            {
                rst = 1;
            }
            else if (rectMax.Contains(pt) == true)
            {
                rst = 2;
            }
            else if (rectClose.Contains(pt) == true)
            {
                rst = 3;
            }

            return rst;
        }
        
    }
}
